﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Windows.PdfViewer;

namespace PdfSdk.PdfViewer
{
    public class SelectionCanvasWrapper : ObjectWrapper
    {
        public static readonly Type s_strikeTypeType;
        private readonly PdfUnitConvertor _unitConvertor = new PdfUnitConvertor();
        private readonly DocumentView _documentView;
        private readonly DocumentViewWrapper _documentViewWrapper;
        private readonly PdfDocumentView _pdfDocumentView;
        private readonly PdfDocumentViewWrapper _pdfDocumentViewWrapper;
        private readonly InkCanvas _canvas;
        private PageWrapper _pageWrapper;

        static SelectionCanvasWrapper()
        {
            Type type = typeof(PdfDocumentView).Assembly.GetType("Syncfusion.Windows.PdfViewer.SelectionCanvas");

            s_strikeTypeType = type.GetNestedType("StrikeType", BindingFlags.NonPublic);
        }

        public SelectionCanvasWrapper(InkCanvas canvas) : base(canvas)
        {
            _canvas = canvas;
            _documentView = (DocumentView)GetFieldValue("m_documentView");
            _documentViewWrapper = _documentView.GetWrapper();
            _pdfDocumentView = _documentView.GetWrapper().m_parent;
            _pdfDocumentViewWrapper = _pdfDocumentView.GetWrapper();

            _pdfDocumentView.PreviewMouseUp += _pdfDocumentView_PreviewMouseUp;
        }

        #region Custom

        public void AddCustomEventHandler()
        {
            _canvas.PreviewMouseDown -= SelectionCanvas_PreviewMouseDown;
            _canvas.PreviewMouseDown += SelectionCanvas_PreviewMouseDown;

            _canvas.MouseDown -= SelectionCanvas_MouseDown;
            _canvas.MouseDown += SelectionCanvas_MouseDown;

            _canvas.MouseMove -= SelectionCanvas_MouseMove;
            _canvas.MouseMove += SelectionCanvas_MouseMove;

            _canvas.MouseUp -= SelectionCanvas_MouseUp;
            _canvas.MouseUp += SelectionCanvas_MouseUp;

            _canvas.Strokes.StrokesChanged -= SelectionCanvas_StrokesChanged;
            _canvas.Strokes.StrokesChanged += SelectionCanvas_StrokesChanged;
        }

        private void _pdfDocumentView_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (_isAreaSelectionMouseDown)
            {
                EndAreaSelection();
            }
        }

        private bool _isAreaSelectionMouseDown;
        private System.Windows.Point _areaSelectionStartingPoint;
        private System.Windows.Point _areaSelectionEndingPoint;
        private System.Windows.Shapes.Rectangle _areaSelectionRectangle;

        private void StartAreaSelection(MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                _areaSelectionStartingPoint = e.GetPosition(_canvas);
                _areaSelectionEndingPoint = _areaSelectionStartingPoint;
                _isAreaSelectionMouseDown = true;
            }
        }

        private void AddAreaSelectionRectangle()
        {
            if (!_isAreaSelectionMouseDown)
                return;

            if (_areaSelectionRectangle == null)
            {
                _areaSelectionRectangle = new System.Windows.Shapes.Rectangle()
                {
                    Stroke = System.Windows.Media.Brushes.Red,
                    StrokeThickness = 1,
                };

                _canvas.Children.Add(_areaSelectionRectangle);
            }

            _areaSelectionEndingPoint = Mouse.GetPosition(_canvas);
            Rect rect = new Rect(_areaSelectionStartingPoint, _areaSelectionEndingPoint);
            _areaSelectionRectangle.Width = rect.Width;
            _areaSelectionRectangle.Height = rect.Height;
            InkCanvas.SetLeft(_areaSelectionRectangle, rect.Left);
            InkCanvas.SetTop(_areaSelectionRectangle, rect.Top);
        }

        private void EndAreaSelection()
        {
            if (!_isAreaSelectionMouseDown)
                return;

            _isAreaSelectionMouseDown = false;

            if (_areaSelectionRectangle != null)
            {
                if (_areaSelectionRectangle.Parent is InkCanvas canvas)
                {
                    canvas.Children.Remove(_areaSelectionRectangle);
                }
                _areaSelectionRectangle = null;
            }

            Rect rect = new Rect(_areaSelectionStartingPoint, _areaSelectionEndingPoint);
            if (rect.Width < 2 && rect.Height < 2)
            {
                return;
            }

            if (_pdfDocumentViewWrapper.AreaSelectionCallback != null)
            {
                _pdfDocumentViewWrapper.AreaSelectionCallback(this, rect);
            }

            _canvas.Dispatcher.InvokeAsync(delegate
            {
                _documentViewWrapper.ClearAnnotationSelection();
                _documentView.CloseSelection();
            });
        }

        private bool _isStrokesChangedCalled;

        private Stroke ToStraight(Stroke stroke)
        {
            if (stroke.StylusPoints.Count < 3)
            {
                return stroke;
            }

            var startPoint = stroke.StylusPoints[0].ToPoint();
            var endPoint = stroke.StylusPoints[stroke.StylusPoints.Count - 1].ToPoint();

            var centerPoint = new System.Windows.Point((startPoint.X + endPoint.X) / 2.0, (startPoint.Y + endPoint.Y) / 2.0);

            StylusPointCollection stylusPoints = new StylusPointCollection()
            {
                new StylusPoint(startPoint.X, startPoint.Y),
                new StylusPoint(centerPoint.X, centerPoint.Y),
                new StylusPoint(endPoint.X, endPoint.Y),
            };

            return new Stroke(stylusPoints);
        }

        #endregion

        public InkCanvas InkCanvas => _originalObject as InkCanvas;

        public DocumentView DocumentView => _documentView;

        public FreeTextAnnotationTextBox m_focusableTextBox
        {
            get => GetFieldValue() as FreeTextAnnotationTextBox;
        }

        public StrikeType MouseStrikeType
        {
            get => (StrikeType)Convert.ToInt32(GetPropertyValue());
            set => SetPropertyValue(Enum.Parse(s_strikeTypeType, value.ToString()));
        }

        internal StrikeType currentFreeTextStrikeType
        {
            get => (StrikeType)Convert.ToInt32(GetFieldValue());
            set => SetFieldValue(Enum.Parse(s_strikeTypeType, value.ToString()));
        }

        private bool m_compositelog
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        private bool m_isRectangleMouseDown
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        internal bool isFreeTextResized
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        internal bool m_canInvokeSelection
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        private bool m_isFromMouseUp
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        private bool m_isRedactionEnded
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        private bool m_isRedactionStarted
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        public PageWrapper _page => _pageWrapper ??= new PageWrapper(GetFieldValue());

        internal bool m_isInkModeEnabled
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }

        public bool m_isDragging
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }
        private bool m_isDragInProgress
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }
        private bool m_isPolygonAdded
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }
        private bool m_isAnnotationResizing
        {
            get => Convert.ToBoolean(GetFieldValue());
            set => SetFieldValue(value);
        }
        private int m_polygonTimeStamp
        {
            get => Convert.ToInt32(GetFieldValue());
            set => SetFieldValue(value);
        }

        private PointF m_freeTextSelctedLastDragPoint
        {
            get => (PointF)GetFieldValue();
            set => SetFieldValue(value);
        }
        public PointF m_lastDragPoint
        {
            get => (PointF)GetFieldValue();
            set => SetFieldValue(value);
        }

        private System.Windows.Point m_LastDragPoint
        {
            get => (System.Windows.Point)GetFieldValue();
            set => SetFieldValue(value);
        }
        private System.Windows.Point mousePoint
        {
            get => (System.Windows.Point)GetFieldValue();
            set => SetFieldValue(value);
        }

        private Rect m_intersectRect
        {
            get => (Rect)GetFieldValue();
            set => SetFieldValue(value);
        }

        public System.Windows.Media.Color m_shapeColor
        {
            get => (System.Windows.Media.Color)GetFieldValue();
            set => SetFieldValue(value);
        }

        public float m_thickness
        {
            get => Convert.ToSingle(GetFieldValue());
            set => SetFieldValue(value);
        }

        public System.Windows.Media.Color m_fillColor
        {
            get => (System.Windows.Media.Color)GetFieldValue();
            set => SetFieldValue(value);

        }
        public float m_opacity
        {
            get => Convert.ToSingle(GetFieldValue());
            set => SetFieldValue(value);
        }

        public int CurrentPageIndex => Convert.ToInt32(GetPropertyValue());

        internal LinkedList<System.Windows.Point> PolylinePoints => GetPropertyValue() as LinkedList<System.Windows.Point>;

        public Rect RemoveRotations(Rect rotatedBounds, double rotatedAngle, SizeF parentSize)
        {
            rotatedBounds = _documentViewWrapper.RemoveRotations(rotatedBounds, rotatedAngle, parentSize);
            return rotatedBounds;
        }

        public PointF RemoveRotations(PointF rotatedPoint, double rotatedAngle, SizeF parentSize)
        {
            if (rotatedAngle == 90.0)
                rotatedPoint = new PointF(rotatedPoint.Y, parentSize.Height - rotatedPoint.X);
            else if (rotatedAngle == 180.0)
                rotatedPoint = new PointF(parentSize.Width - rotatedPoint.X, parentSize.Height - rotatedPoint.Y);
            else if (rotatedAngle == 270.0)
                rotatedPoint = new PointF(parentSize.Width - rotatedPoint.Y, rotatedPoint.X);
            return rotatedPoint;
        }

        public bool RedactionOnMouseMove(PointF currentPoint)
        {
            return Convert.ToBoolean(InvokeMethod("RedactionOnMouseMove", currentPoint));
        }

        public StrikeType DetectInkOnMove(System.Windows.Input.MouseEventArgs e)
        {
            var obj = InvokeMethod("", new Type[] { typeof(System.Windows.Input.MouseEventArgs) }, e);
            return (StrikeType)Convert.ToInt32(obj);
        }

        internal StrikeType DetectStrokes(PointF currentPoint, bool isOnMouseMove)
        {
            return (StrikeType)Convert.ToInt32(InvokeMethod("DetectStrokes", currentPoint, isOnMouseMove));
        }

        internal void AddSelectBounds()
        {
            InvokeMethod();
        }

        private RectangleF UpdateRectangleOnZoom(RectangleF tempRectangle)
        {
            tempRectangle.X *= (float)_documentView.ZoomFactor;
            tempRectangle.Y *= (float)_documentView.ZoomFactor;
            tempRectangle.Width *= (float)_documentView.ZoomFactor;
            tempRectangle.Height *= (float)_documentView.ZoomFactor;
            return tempRectangle;
        }

        private Rect GetRectangleOnZoom(Rect tempRectangle)
        {
            tempRectangle.X *= _documentView.ZoomFactor;
            tempRectangle.Y *= _documentView.ZoomFactor;
            tempRectangle.Width *= _documentView.ZoomFactor;
            tempRectangle.Height *= _documentView.ZoomFactor;
            return tempRectangle;
        }

        private PointF GetPointOnZoom(PointF point)
        {
            point.X *= (float)_documentView.ZoomFactor;
            point.Y *= (float)_documentView.ZoomFactor;
            return point;
        }

        private PointF GetNormalPoint(PointF point)
        {
            point.X /= (float)_documentView.ZoomFactor;
            point.Y /= (float)_documentView.ZoomFactor;
            return point;
        }

        private RectangleF GetNormalSizedRectangle(RectangleF rectangle)
        {
            rectangle.X /= (float)_documentView.ZoomFactor;
            rectangle.Y /= (float)_documentView.ZoomFactor;
            rectangle.Width /= (float)_documentView.ZoomFactor;
            rectangle.Height /= (float)_documentView.ZoomFactor;
            return rectangle;
        }

        private StrikeType SetStrikeType(Stroke stroke, PointF currentPoint)
        {
            double num = 2.0 * _documentView.ZoomFactor;
            for (int index = 0; index < stroke.StylusPoints.Count; ++index)
            {
                double x = stroke.StylusPoints[index].X;
                double y = stroke.StylusPoints[index].Y;
                if ((double)currentPoint.X >= x - num && (double)currentPoint.X <= x + num)
                {
                    if ((double)currentPoint.Y >= y - num && (double)currentPoint.Y <= y + num)
                        return StrikeType.Body;
                }
                else if ((double)currentPoint.Y >= y - num && (double)currentPoint.Y <= y + num && (double)currentPoint.X >= x - num && (double)currentPoint.X <= x + num)
                    return StrikeType.Body;
            }
            return StrikeType.None;
        }

        private void SetExpectedCursor()
        {
            InvokeMethod();
        }

        public StrikeType SetStrikeType(PointF startPoint, PointF endPoint, PointF point, DocumentViewWrapper.PdfViewerShape shape)
        {
            Type enumType = DocumentViewWrapper.s_pdfViewerShapeType;
            var obj = InvokeMethod("SetStrikeType",
                new Type[] { typeof(PointF), typeof(PointF), typeof(PointF), enumType },
                startPoint, endPoint, point, Enum.Parse(enumType, shape.ToString()));
            return (StrikeType)Convert.ToInt32(obj);
        }

        internal double GetDistanceBetweenPoints(System.Windows.Point currentTapPosition, System.Windows.Point lastTapPosition)
        {
            return Convert.ToDouble(InvokeMethod("GetDistanceBetweenPoints", currentTapPosition, lastTapPosition));
        }

        private bool DetectPolygon(EventArgs e) => Convert.ToBoolean(InvokeMethod("DetectPolygon", e));
        private bool DetectStickyNote(EventArgs e) => Convert.ToBoolean(InvokeMethod("DetectStickyNote", e));
        private StrikeType DetectShapes(PointF currentPoint)
        {
            return (StrikeType)Convert.ToInt32(InvokeMethod("DetectShapes", new Type[] { typeof(PointF) }, currentPoint));
        }

        private bool IsInsideAnnotationBounds(EventArgs e) => Convert.ToBoolean(InvokeMethod("IsInsideAnnotationBounds", e));
        private void AddPolygon() => InvokeMethod();
        private bool IsPolygonDoubleTapped(EventArgs eventArgs) => Convert.ToBoolean(InvokeMethod("IsPolygonDoubleTapped", eventArgs));

        internal Rect RotateBounds(Rect rotatingBounds, double angleToRotate, SizeF parentSize, bool preMarkRegion)
        {
            var obj = InvokeMethod("RotateBounds",
                new Type[] { typeof(Rect), typeof(double), typeof(SizeF), typeof(bool) },
                rotatingBounds, angleToRotate, parentSize, preMarkRegion);
            return (Rect)obj;
        }

        internal Rect RotateBounds(Rect rotatingBounds, double angleToRotate, SizeF parentSize)
        {
            var obj = InvokeMethod("RotateBounds",
               new Type[] { typeof(Rect), typeof(double), typeof(SizeF) },
               rotatingBounds, angleToRotate, parentSize);
            return (Rect)obj;
        }

        private void AddStickyNote(EventArgs e) => InvokeMethod("AddStickyNote", e);

        internal void AddStamp(MouseEventArgs e)
        {
            InvokeMethod("AddStamp", new Type[] { typeof(MouseEventArgs) }, e);
        }

        private void AddInkSignature(MouseEventArgs e)
        {
            InvokeMethod("AddInkSignature", new Type[] { typeof(MouseEventArgs) }, e);
        }

        private bool IsDoubleTap(MouseEventArgs e)
        {
            return Convert.ToBoolean(InvokeMethod("AddInkSignature", new Type[] { typeof(MouseEventArgs) }, e));
        }

        private void AddTextBox(System.Windows.Point mousePositionPixel)
        {
            InvokeMethod("AddTextBox", mousePositionPixel);
        }

        public void AddMarkupAnnotations(PdfTextMarkupAnnotationType markupAnnotationType)
        {
            InvokeMethod("AddMarkupAnnotations", markupAnnotationType);
        }

        internal void DeselectFreeTextAnnotation() => InvokeMethod();

        private void AddPolyline() => InvokeMethod();

        internal void AddShapes(int pageIndex, DocumentViewWrapper.PdfViewerShape shapeName)
        {
            InvokeMethod("AddShapes", pageIndex, Enum.Parse(DocumentViewWrapper.s_pdfViewerShapeType, shapeName.ToString()));
        }

        internal void AdjustXPosition() => InvokeMethod();

        private void AddInk() => InvokeMethod();

        private void SelectInk(TouchEventArgs e)
        {
            InvokeMethod("SelectInk", new Type[] { typeof(TouchEventArgs) }, e);
        }

        private void SelectInk(MouseButtonEventArgs e)
        {
            InvokeMethod("SelectInk", new Type[] { typeof(MouseButtonEventArgs) }, e);
        }

        private void StartTextSelection(PointF currentPoint)
        {
            InvokeMethod("StartTextSelection", currentPoint);
        }

        private void InitiateSelectionExpansion(Ellipse ellipse) => InvokeMethod("InitiateSelectionExpansion", ellipse);

        private void TextSelectionOnTapped(System.Windows.Point positionPixel) => InvokeMethod("TextSelectionOnTapped", positionPixel);

        private void SelectionCanvas_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
        {
            if (_isStrokesChangedCalled)
                return;

            if (_canvas.EditingMode == InkCanvasEditingMode.Select && _canvas.GetSelectedStrokes().Count == 0)
            {
                _canvas.EditingMode = InkCanvasEditingMode.None;
                _canvas.InvalidateVisual();
            }
            if (_canvas.EditingMode == InkCanvasEditingMode.None || _canvas.EditingMode == InkCanvasEditingMode.Select || _canvas.EditingMode != InkCanvasEditingMode.Ink || _documentViewWrapper.SelectedAnnotation != null)
                return;

            if (_pdfDocumentViewWrapper.InkStraightEnabled)
            {
                if (e.Added.Count > 0 && e.Added.Last().StylusPoints.Count > 3)
                {
                    try
                    {
                        _isStrokesChangedCalled = true;

                        var stroke = ToStraight(e.Added.Last());
                        _canvas.Strokes.Clear();
                        _canvas.Strokes.Add(stroke);
                        AddInk();
                    }
                    finally
                    {
                        _isStrokesChangedCalled = false;
                    }
                    return;
                }
            }

            this.AddInk();
        }

        internal void SelectionCanvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.InkEraser)
                return;
            UIElement relativeTo = _canvas;
            if (_pdfDocumentViewWrapper.IsPanning && _documentViewWrapper.IsTouchTextSelectionEnabled)
                _documentViewWrapper.DisableTouchTextSelection();
            else if (_documentViewWrapper.IsTouchTextSelectionEnabled)
            {
                if (e.OriginalSource is Ellipse originalSource)
                {
                    InitiateSelectionExpansion(originalSource);
                    return;
                }
                TextSelectionOnTapped(e.GetPosition(relativeTo));
                return;
            }
            System.Windows.Point position1 = e.GetPosition(relativeTo);
            _documentViewWrapper.m_mousePosition = position1;
            PointF pointF1 = _unitConvertor.ConvertFromPixels(new PointF((float)position1.X, (float)e.GetPosition(_canvas).Y), PdfGraphicsUnit.Point);
            m_LastDragPoint = position1;
            if (_canvas.EditingMode == InkCanvasEditingMode.Ink || m_isInkModeEnabled)
            {
                SelectInk(e);

                #region Custom

                if (!_documentViewWrapper.IsInkSelected)
                {
                    _pdfDocumentViewWrapper.PdfViewer.DeselectInkAnnotation();
                }

                #endregion
            }
            else
            {
                if (_page.Rotation == 90.0)
                    pointF1 = RemoveRotations(pointF1, 90.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                else if (_page.Rotation == 180.0)
                    pointF1 = RemoveRotations(pointF1, 180.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                else if (_page.Rotation == 270.0)
                    pointF1 = RemoveRotations(pointF1, 270.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                m_freeTextSelctedLastDragPoint = pointF1;
                mousePoint = e.GetPosition(_canvas);
                if (_canvas.EditingMode == InkCanvasEditingMode.Ink && _canvas.GetSelectedStrokes().Count == 0)
                {
                    System.Windows.Point position2 = Mouse.GetPosition(_canvas);
                    PointF rotatedPoint = _unitConvertor.ConvertFromPixels(new PointF((float)position2.X, (float)position2.Y), PdfGraphicsUnit.Point);
                    PointF pointF2;
                    if (_page.Rotation == 90.0)
                        pointF2 = RemoveRotations(rotatedPoint, 90.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                    else if (_page.Rotation == 180.0)
                        pointF2 = RemoveRotations(rotatedPoint, 180.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                    else if (_page.Rotation == 270.0)
                        pointF2 = RemoveRotations(rotatedPoint, 270.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                    int num = (int)DetectStrokes(new PointF((float)position2.X, (float)position2.Y), false);
                }
                if (e.ChangedButton == MouseButton.Right)
                    return;
                if (_canvas.EditingMode == InkCanvasEditingMode.Select)
                {
                    if (!_documentViewWrapper.TextAnnotations.ContainsKey(CurrentPageIndex))
                        return;
                    IList textAnnotation = _documentViewWrapper.TextAnnotations[CurrentPageIndex];
                    for (int index = 0; index < textAnnotation.Count; ++index)
                    {
                        PdfViewerFreeTextAnnotationWrapper freeTextAnnotationWrapper = new(textAnnotation[index]);
                        if (freeTextAnnotationWrapper.Bounds[0].Contains((float)position1.X, (float)position1.Y))
                            new FreeTextAnnotationTextBoxWrapper(freeTextAnnotationWrapper.TextBox).PVTextBox_PreviewMouseDown(sender, e);
                    }
                }
                else
                {
                    if (m_isInkModeEnabled || MouseStrikeType != StrikeType.None)
                        return;
                    if (_pdfDocumentViewWrapper.InkMode != PdfDocumentViewWrapper.PdfViewerInkAnnotationMode.None)
                    {
                        if (_documentViewWrapper.SelectedAnnotation == null)
                            return;
                        if (_documentViewWrapper.SelectedAnnotation is PdfViewerFreeTextAnnotationWrapper)
                        {
                            ((_documentViewWrapper.SelectedAnnotation as PdfViewerFreeTextAnnotationWrapper).TextBox.Parent as InkCanvas).GetWrapper().DeselectFreeTextAnnotation();
                            _documentViewWrapper.ClearFreeTextSelection();
                        }
                        else
                        {
                            if (!(_documentViewWrapper.SelectedAnnotation is PdfViewerInkAnnotationWrapper))
                                _documentViewWrapper.SelectedAnnotation = null;
                            _canvas.InvalidateVisual();
                        }
                    }
                    else
                    {
                        if (e.ChangedButton == MouseButton.Right)
                            return;
                        if (_documentViewWrapper.SelectedRegion?.AnnotationPopup?.AnnotationPopup?.IsOpen == true)
                            return;
                        bool flag = false;
                        if (_documentViewWrapper.MouseOveredRegion != null)
                        {
                            foreach (RectangleF bound in _documentViewWrapper.MouseOveredRegion.Bounds)
                            {
                                if (bound.Contains(pointF1.X / (float)_documentView.ZoomFactor, pointF1.Y / (float)_documentView.ZoomFactor))
                                    flag = true;
                            }
                        }
                        if (flag)
                        {
                            _documentViewWrapper.SelectedRegion = null;
                            _documentViewWrapper.SelectedRegion = _documentViewWrapper.MouseOveredRegion.Clone();
                            _canvas.InvalidateVisual();
                        }
                        else
                        {
                            if (_documentViewWrapper.SelectedRegion != null)
                                _documentViewWrapper.SelectedRegion = null;
                            if (_pdfDocumentViewWrapper.ShapeMode != PdfDocumentViewWrapper.PdfViewerShapeAnnotationMode.None)
                            {
                                if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation.AnnotationPopup != null && _documentViewWrapper.SelectedAnnotation.AnnotationPopup.AnnotationPopup != null && _documentViewWrapper.SelectedAnnotation.AnnotationPopup.AnnotationPopup.IsOpen)
                                    return;
                                if (_documentViewWrapper.TextAnnotations.ContainsKey(CurrentPageIndex))
                                {
                                    IList textAnnotation = _documentViewWrapper.TextAnnotations[CurrentPageIndex];
                                    for (int index = 0; index < textAnnotation.Count; ++index)
                                    {
                                        PdfViewerFreeTextAnnotationWrapper freeTextAnnotationWrapper = new(textAnnotation[index]);
                                        if (freeTextAnnotationWrapper.Bounds[0].Contains((float)position1.X, (float)position1.Y))
                                        {
                                            new FreeTextAnnotationTextBoxWrapper(freeTextAnnotationWrapper.TextBox).PVTextBox_PreviewMouseDown(freeTextAnnotationWrapper.TextBox, e);
                                            return;
                                        }
                                    }
                                }
                                _documentViewWrapper.StartingPoint = GetNormalPoint(pointF1);
                                _documentViewWrapper.CanvasPageIndex = CurrentPageIndex;
                            }
                            else if (_pdfDocumentView.PageRedactor.EnableRedactionMode && !flag)
                            {
                                m_isRedactionStarted = true;
                                m_isRedactionEnded = false;
                                _documentViewWrapper.StartingPoint = GetNormalPoint(pointF1);
                                _documentViewWrapper.CanvasPageIndex = CurrentPageIndex;
                                if (_canvas.Cursor != System.Windows.Input.Cursors.Cross)
                                    return;
                                m_isDragInProgress = true;
                                _documentViewWrapper.IsDragInProgress = true;
                            }
                            else
                                StartTextSelection(pointF1);
                        }
                    }
                }
            }
        }

        private void SelectionCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.InkEraser)
                return;

            var pageIndex = CurrentPageIndex;

            System.Windows.Point position = e.GetPosition(_canvas);
            if (_documentView != null && e.Source?.GetType()?.Name == "SelectionCanvas")
                _documentViewWrapper.RemoveAddedStickySelection();
            _documentViewWrapper.IsStampAdded = false;
            _pdfDocumentViewWrapper.HideFontPopUp();
            if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.Polygon
                && !m_isRectangleMouseDown
                && e.ChangedButton != MouseButton.Right
                && (_documentViewWrapper.PolygonPoints.Count != 0
                    || !DetectPolygon(e)
                    && MouseStrikeType == StrikeType.None
                    && !DetectStickyNote(e)
                    && !IsInsideAnnotationBounds(e)))
            {
                _documentViewWrapper.SetScrollbarEnableState("PART_HorizontalScrollBar", false);
                _documentViewWrapper.SetScrollbarEnableState("PART_VerticalScrollBar", false);
                if (_documentViewWrapper.PolygonPageIndex == -1)
                    _documentViewWrapper.PolygonPageIndex = pageIndex;
                if (_documentViewWrapper.PolygonPageIndex == pageIndex)
                {
                    if (_documentViewWrapper.PolygonDoubleTappedPoint.X != 0.0 && _documentViewWrapper.PolygonDoubleTappedPoint.Y != 0.0)
                    {
                        bool flag1 = GetDistanceBetweenPoints(_documentViewWrapper.PolygonDoubleTappedPoint, e.GetPosition(_canvas)) < 40.0;
                        bool flag2 = e.Timestamp - m_polygonTimeStamp < 500;
                        if (flag1 && flag2)
                        {
                            _documentViewWrapper.PolygonDoubleTappedPoint = new System.Windows.Point();
                        }
                        else
                        {
                            _documentViewWrapper.PolygonPoints.AddLast(e.GetPosition(_canvas));
                            _documentViewWrapper.PolygonDoubleTappedPoint = new System.Windows.Point();
                        }
                    }
                    else
                        _documentViewWrapper.PolygonPoints.AddLast(e.GetPosition(_canvas));
                    if (_documentViewWrapper.PolygonPoints.Count > 2)
                    {
                        List<System.Windows.Point> list = _documentViewWrapper.PolygonPoints.ToList();
                        m_intersectRect = new Rect(
                            list[0].X - _documentViewWrapper.PolygonStartRectangleWidth,
                            list[0].Y - _documentViewWrapper.PolygonStartRectangleWidth,
                            _documentViewWrapper.PolygonStartRectangleWidth * 2.5,
                            _documentViewWrapper.PolygonStartRectangleWidth * 2.5);
                        if (m_intersectRect.Contains(list[list.Count - 1]))
                        {
                            m_isPolygonAdded = true;
                            _documentViewWrapper.PolygonDoubleTappedPoint = list[list.Count - 1];
                            m_polygonTimeStamp = e.Timestamp;
                            _documentViewWrapper.PolygonPoints.RemoveLast();
                            AddPolygon();
                            return;
                        }
                        if (IsPolygonDoubleTapped(e))
                        {
                            _documentViewWrapper.PolygonPoints.RemoveLast();
                            AddPolygon();
                            return;
                        }
                    }
                }
            }
            if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.StickyNote
                && _canvas.Cursor == LocalizationManagerWrapper.StickyNoteCursor
                && MouseStrikeType == StrikeType.None
                && !_documentViewWrapper.IsAnnotationHovered
                && !m_isRectangleMouseDown
                && !DetectStickyNote(e))
            {
                _documentViewWrapper.ClearControlAnnotationSelection();
                if (_documentViewWrapper.IsShapeSelected)
                {
                    _documentView.SelectedBounds.Clear();
                    _documentViewWrapper.ClearSelection();
                    _documentViewWrapper.imageProvider._selectionCanvas.InvalidateVisual();
                }
                if (_documentViewWrapper.SelectedAnnotation != null)
                {
                    if (_documentViewWrapper.SelectedAnnotation is PdfViewerStickyNoteAnnotationWrapper selectedAnnotation)
                    {
                        SizeF parentSize = new SizeF(_unitConvertor.ConvertToPixels(_documentView.LoadedDocument.Pages[selectedAnnotation.PageIndex].Size, PdfGraphicsUnit.Point));
                        Rect rotatingBounds = new Rect(
                            (double)selectedAnnotation.Bounds[0].X,
                            (double)selectedAnnotation.Bounds[0].Y,
                            (double)selectedAnnotation.Bounds[0].Width,
                            (double)selectedAnnotation.Bounds[0].Height);
                        if (_documentViewWrapper.PagesEx[selectedAnnotation.PageIndex].CanvasEx
                            .RotateBounds(rotatingBounds, _documentViewWrapper.PagesEx[selectedAnnotation.PageIndex].Rotation, parentSize)
                            .Contains(position.X / _documentView.ZoomFactor, position.Y / _documentView.ZoomFactor)
                            || currentFreeTextStrikeType != StrikeType.None
                            || m_isRectangleMouseDown)
                            return;
                        _documentViewWrapper.ClearStickyNoteSelection();
                        AddStickyNote(e);
                    }
                    else
                        AddStickyNote(e);
                }
                else
                    AddStickyNote(e);
            }
            else if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.Stamp && (_pdfDocumentViewWrapper.IsStandardStampSelected || _pdfDocumentViewWrapper.IsCustomImageSelected))
            {
                AddStamp(e);
            }
            else
            {
                if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.HandwrittenSignature)
                {
                    AddInkSignature(e);
                }
                else
                {
                    if (_documentView != null && _pdfDocumentView != null)
                    {
                        PageClickedEventArgs eventArgs = CreateInstance(
                            typeof(PageClickedEventArgs),
                            pageIndex,
                            e.GetPosition(_canvas)) as PageClickedEventArgs;
                        _pdfDocumentViewWrapper.OnPageClicked(eventArgs);
                    }
                    PointF pointF = _unitConvertor.ConvertFromPixels(new PointF((float)position.X, (float)e.GetPosition(_canvas).Y), PdfGraphicsUnit.Point);
                    if (_page.Rotation == 90.0)
                        pointF = RemoveRotations(pointF, 90.0, new SizeF(_documentView.LoadedDocument.Pages[pageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[pageIndex].Size.Height * (float)_documentView.ZoomFactor));
                    else if (_page.Rotation == 180.0)
                        pointF = RemoveRotations(pointF, 180.0, new SizeF(_documentView.LoadedDocument.Pages[pageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[pageIndex].Size.Height * (float)_documentView.ZoomFactor));
                    else if (_page.Rotation == 270.0)
                        pointF = RemoveRotations(pointF, 270.0, new SizeF(_documentView.LoadedDocument.Pages[pageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[pageIndex].Size.Height * (float)_documentView.ZoomFactor));

                    if (_pdfDocumentViewWrapper.TextMarkupMode == PdfDocumentViewWrapper.PdfViewerTextMarkupAnnotationMode.None && _pdfDocumentViewWrapper.InkMode == PdfDocumentViewWrapper.PdfViewerInkAnnotationMode.None && (e.ChangedButton != MouseButton.Right) && !_documentViewWrapper.IsPolylineInProgress && _documentViewWrapper.PolygonPoints.Count == 0)
                    {
                        int num1 = (int)DetectShapes(pointF);
                    }
                    if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.Polyline && !(e.OriginalSource is Polyline) && !(_documentViewWrapper.SelectedAnnotation is PolylineAnnotationWrapper) && !(e.OriginalSource is System.Windows.Shapes.Rectangle) && !(e.OriginalSource is Path) && !(e.OriginalSource is Border) && !(e.OriginalSource?.GetType()?.Name == "CustomPolygon") && e.ChangedButton != MouseButton.Right && MouseStrikeType == StrikeType.None)
                    {
                        _documentViewWrapper.SetScrollbarEnableState("PART_HorizontalScrollBar", false);
                        _documentViewWrapper.SetScrollbarEnableState("PART_VerticalScrollBar", false);
                        DeselectFreeTextAnnotation();
                        if (_documentViewWrapper.PolylinePageIndex == -1)
                            _documentViewWrapper.PolylinePageIndex = pageIndex;
                        if (_documentViewWrapper.PolylinePageIndex == pageIndex)
                        {
                            PolylinePoints.AddLast(e.GetPosition(_canvas));
                            if (PolylinePoints.Count > 0)
                                _documentViewWrapper.IsPolylineInProgress = true;
                            if (PolylinePoints.Count > 2 && IsDoubleTap(e))
                            {
                                AddPolyline();
                                _documentViewWrapper.SetScrollbarEnableState("PART_HorizontalScrollBar", true);
                                _documentViewWrapper.SetScrollbarEnableState("PART_VerticalScrollBar", true);
                                return;
                            }
                        }
                    }
                    if (_documentViewWrapper.CurrentAnnotation != null && !_documentViewWrapper.CurrentAnnotation.Hidden && !_documentViewWrapper.CurrentAnnotation.IsLocked && !_documentViewWrapper.CurrentAnnotation.ReadOnly)
                        _documentView.SelectedBounds.Clear();
                    if (!m_isRectangleMouseDown && _documentViewWrapper.PolygonPoints.Count == 0 && PolylinePoints.Count == 0)
                    {
                        if (_pdfDocumentViewWrapper.TextMarkupMode != PdfDocumentViewWrapper.PdfViewerTextMarkupAnnotationMode.None)
                            MouseStrikeType = StrikeType.None;
                        if (_pdfDocumentView.AnnotationMode != PdfDocumentView.PdfViewerAnnotationMode.InkEraser)
                            SetExpectedCursor();
                    }
                    if (_documentViewWrapper.m_selectedFreeTextAnnotation != null && !_documentViewWrapper.m_selectedFreeTextAnnotation.Bounds[0].Contains((float)(position.X / _documentView.ZoomFactor), (float)(position.Y / _documentView.ZoomFactor)) && currentFreeTextStrikeType == StrikeType.None && !m_isRectangleMouseDown && !_documentViewWrapper.m_selectedFreeTextAnnotation.isPopUpWindowOpened)
                    {
                        _documentViewWrapper.IsFreeTextSelected = false;
                        if (_documentViewWrapper.m_selectedFreeTextAnnotation.TextBox.Parent != null)
                            (_documentViewWrapper.m_selectedFreeTextAnnotation.TextBox.Parent as InkCanvas).GetWrapper().DeselectFreeTextAnnotation();
                        _documentViewWrapper.ClearFreeTextSelection();
                        _documentViewWrapper.PreviousShapeAnnotation = null;
                        _documentViewWrapper.CurrentAnnotations = null;
                        _documentViewWrapper.PreviousAnnotation = null;
                    }
                    if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation is PdfViewerStampAnnotationWrapper)
                    {
                        PdfViewerStampAnnotationWrapper selectedAnnotation = _documentViewWrapper.SelectedAnnotation as PdfViewerStampAnnotationWrapper;
                        SizeF parentSize = new SizeF(new PdfUnitConvertor().ConvertToPixels(_documentView.LoadedDocument.Pages[selectedAnnotation.PageIndex].Size, PdfGraphicsUnit.Point));
                        Rect rotatingBounds = new Rect((double)selectedAnnotation.Bounds[0].X, (double)selectedAnnotation.Bounds[0].Y, (double)selectedAnnotation.Bounds[0].Width, (double)selectedAnnotation.Bounds[0].Height);
                        rotatingBounds = _documentViewWrapper.PagesEx[selectedAnnotation.PageIndex].CanvasEx.RotateBounds(rotatingBounds, _documentViewWrapper.PagesEx[selectedAnnotation.PageIndex].Rotation, parentSize);
                        if (!rotatingBounds.Contains(position.X / _documentView.ZoomFactor, position.Y / _documentView.ZoomFactor))
                        {
                            if (selectedAnnotation.StampGrid.Parent != null && currentFreeTextStrikeType == StrikeType.None && !m_isRectangleMouseDown)
                            {
                                (selectedAnnotation.StampGrid.Parent as InkCanvas).GetWrapper().DeselectFreeTextAnnotation();
                                _documentViewWrapper.ClearSelection();
                                _documentViewWrapper.SelectedAnnotation = null;
                                _documentViewWrapper.PreviousShapeAnnotation = null;
                                _documentViewWrapper.CurrentAnnotations = null;
                                _documentViewWrapper.PreviousAnnotation = null;
                            }
                        }
                        else if (selectedAnnotation.Bounds[0].Contains((float)(position.X / _documentView.ZoomFactor), (float)(position.Y / _documentView.ZoomFactor)))
                            return;
                    }
                    if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation is PolylineAnnotationWrapper)
                    {
                        PolylineAnnotationWrapper selectedAnnotation = _documentViewWrapper.SelectedAnnotation as PolylineAnnotationWrapper;
                        if (!new Rect((double)selectedAnnotation.Bounds[0].X, (double)selectedAnnotation.Bounds[0].Y, (double)selectedAnnotation.Bounds[0].Width, (double)selectedAnnotation.Bounds[0].Height).Contains(position.X, position.Y))
                        {
                            if (currentFreeTextStrikeType == StrikeType.None && !m_isRectangleMouseDown)
                            {
                                if (selectedAnnotation.Polyline != null && selectedAnnotation.Polyline.Parent != null)
                                    (selectedAnnotation.Polyline.Parent as InkCanvas).GetWrapper().DeselectFreeTextAnnotation();
                                _documentViewWrapper.ClearPolylineSelection();
                                _documentViewWrapper.SelectedAnnotation = null;
                                _documentViewWrapper.PreviousShapeAnnotation = null;
                                _documentViewWrapper.CurrentAnnotations = null;
                                _documentViewWrapper.PreviousAnnotation = null;
                            }
                        }
                        else if (selectedAnnotation.Bounds[0].Contains((float)(position.X / _documentView.ZoomFactor), (float)(position.Y / _documentView.ZoomFactor)))
                            return;
                    }
                    if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation.PageIndex == pageIndex && _documentViewWrapper.SelectedAnnotation is PdfViewerStickyNoteAnnotationWrapper selectedAnnotation1)
                    {
                        SizeF parentSize = new SizeF(_unitConvertor.ConvertToPixels(_documentView.LoadedDocument.Pages[selectedAnnotation1.PageIndex].Size, PdfGraphicsUnit.Point));
                        Rect rotatingBounds = new Rect((double)selectedAnnotation1.Bounds[0].X, (double)selectedAnnotation1.Bounds[0].Y, (double)selectedAnnotation1.Bounds[0].Width / _documentView.ZoomFactor, (double)selectedAnnotation1.Bounds[0].Height / _documentView.ZoomFactor);
                        if (!_documentViewWrapper.PagesEx[selectedAnnotation1.PageIndex].CanvasEx.RotateBounds(rotatingBounds, _documentViewWrapper.PagesEx[selectedAnnotation1.PageIndex].Rotation, parentSize).Contains(position.X / _documentView.ZoomFactor, position.Y / _documentView.ZoomFactor))
                        {
                            if (currentFreeTextStrikeType == StrikeType.None && !m_isRectangleMouseDown && selectedAnnotation1.AnnotationPopup != _documentViewWrapper.DraggedAnnotationPopup)
                                _documentViewWrapper.ClearStickyNoteSelection();
                            _documentViewWrapper.PreviousShapeAnnotation = null;
                            _documentViewWrapper.CurrentAnnotations = null;
                            _documentViewWrapper.PreviousAnnotation = null;
                        }
                        else if (selectedAnnotation1.Bounds[0].Contains((float)(position.X / _documentView.ZoomFactor), (float)(position.Y / _documentView.ZoomFactor)))
                            return;
                    }
                    if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation is PdfViewerPolygonAnnotationWrapper selectedAnnotation2 && selectedAnnotation2.Polygon != null)
                    {
                        if (!selectedAnnotation2.Polygon.RenderedGeometry.Bounds.Contains(position.X, position.Y))
                        {
                            if (currentFreeTextStrikeType == StrikeType.None && !m_isRectangleMouseDown)
                            {
                                _documentViewWrapper.ClearPolygonSelection();
                                _documentViewWrapper.PreviousShapeAnnotation = null;
                                _documentViewWrapper.PreviousAnnotation = null;
                            }
                        }
                        else if (selectedAnnotation2.Polygon.RenderedGeometry.Bounds.Contains(position.X, position.Y))
                            return;
                    }
                    if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.FreeText && _pdfDocumentViewWrapper.TextMarkupMode == PdfDocumentViewWrapper.PdfViewerTextMarkupAnnotationMode.None && _pdfDocumentViewWrapper.InkMode == PdfDocumentViewWrapper.PdfViewerInkAnnotationMode.None && _pdfDocumentViewWrapper.ShapeMode == PdfDocumentViewWrapper.PdfViewerShapeAnnotationMode.None && !_documentViewWrapper.IsShapeSelected)
                    {
                        _documentViewWrapper.ClearFreeTextSelection();
                        AddTextBox(position);
                    }
                    else
                    {
                        if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation is InkSignatureWrapper)
                        {
                            InkSignatureWrapper selectedAnnotation3 = _documentViewWrapper.SelectedAnnotation as InkSignatureWrapper;
                            SizeF parentSize = new SizeF(new PdfUnitConvertor().ConvertToPixels(_documentView.LoadedDocument.Pages[selectedAnnotation3.PageIndex].Size, PdfGraphicsUnit.Point));
                            Rect rotatingBounds = new Rect((double)selectedAnnotation3.Bounds[0].X, (double)selectedAnnotation3.Bounds[0].Y, (double)selectedAnnotation3.Bounds[0].Width, (double)selectedAnnotation3.Bounds[0].Height);
                            rotatingBounds = _documentViewWrapper.PagesEx[selectedAnnotation3.PageIndex].CanvasEx.RotateBounds(rotatingBounds, _documentViewWrapper.PagesEx[selectedAnnotation3.PageIndex].Rotation, parentSize);
                            if (!rotatingBounds.Contains(position.X / _documentView.ZoomFactor, position.Y / _documentView.ZoomFactor))
                            {
                                if (currentFreeTextStrikeType == StrikeType.None && !m_isRectangleMouseDown)
                                {
                                    (selectedAnnotation3.Border.Parent as InkCanvas).GetWrapper().DeselectFreeTextAnnotation();
                                    _documentViewWrapper.ClearSelection();
                                    HandwrittenSignatureChangedEventArgs e1 = CreateInstance(
                                        typeof(HandwrittenSignatureChangedEventArgs),
                                        selectedAnnotation3.PageIndex,
                                        AnnotationChangedAction.Deselect,
                                        selectedAnnotation3.OldBounds,
                                        selectedAnnotation3.Bounds,
                                        selectedAnnotation3.OldBackgroundColor,
                                        selectedAnnotation3.BackgroundColor,
                                        selectedAnnotation3.OldOpacity,
                                        selectedAnnotation3.Opacity,
                                        selectedAnnotation3.SignaturePath) as HandwrittenSignatureChangedEventArgs;
                                    _pdfDocumentViewWrapper.OnHandWrittenSignatureChanged(e1);
                                    _documentViewWrapper.SelectedAnnotation = null;
                                    _documentViewWrapper.PreviousShapeAnnotation = null;
                                    _documentViewWrapper.CurrentAnnotations = null;
                                    _documentViewWrapper.PreviousAnnotation = null;
                                    _documentViewWrapper.IsAnnotationSelected = false;
                                }
                            }
                            else if (rotatingBounds.Contains(position.X / _documentView.ZoomFactor, position.Y / _documentView.ZoomFactor))
                                return;
                        }
                        if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation is PdfViewerInkAnnotationWrapper selectedAnnotation4)
                        {
                            SizeF parentSize = new SizeF(new PdfUnitConvertor().ConvertToPixels(_documentView.LoadedDocument.Pages[selectedAnnotation4.PageIndex].Size, PdfGraphicsUnit.Point));
                            Rect rotatingBounds = new Rect((double)selectedAnnotation4.Bounds[0].X, (double)selectedAnnotation4.Bounds[0].Y, (double)selectedAnnotation4.Bounds[0].Width, (double)selectedAnnotation4.Bounds[0].Height);
                            rotatingBounds = _documentViewWrapper.PagesEx[selectedAnnotation4.PageIndex].CanvasEx.RotateBounds(rotatingBounds, _documentViewWrapper.PagesEx[selectedAnnotation4.PageIndex].Rotation, parentSize);
                            if (!rotatingBounds.Contains(position.X / _documentView.ZoomFactor, position.Y / _documentView.ZoomFactor))
                            {
                                if (currentFreeTextStrikeType == StrikeType.None && !m_isRectangleMouseDown)
                                {
                                    (selectedAnnotation4.Border.Parent as InkCanvas).GetWrapper().DeselectFreeTextAnnotation();
                                    _documentViewWrapper.ClearSelection();
                                    _documentViewWrapper.SelectedAnnotation = null;
                                    _documentViewWrapper.PreviousShapeAnnotation = null;
                                    _documentViewWrapper.CurrentAnnotations = null;
                                    _documentViewWrapper.PreviousAnnotation = null;
                                }
                            }
                            else if (rotatingBounds.Contains(position.X / _documentView.ZoomFactor, position.Y / _documentView.ZoomFactor))
                                return;
                        }
                        if (MouseStrikeType == StrikeType.None)
                        {
                            _documentViewWrapper.IsShapeSelected = false;
                            if (_documentView != null && _documentViewWrapper.PagesEx != null)
                            {
                                for (int key = 0; key < _documentViewWrapper.PagesEx.Count; ++key)
                                {
                                    if (_documentViewWrapper.Shapes.ContainsKey(key) && _documentViewWrapper.PagesEx.ContainsKey(key) && _documentViewWrapper.PagesEx[key].CanvasEx != null)
                                        _documentViewWrapper.PagesEx[key].Canvas.InvalidateVisual();
                                }
                            }
                            if ((_pdfDocumentViewWrapper.TextMarkupMode == PdfDocumentViewWrapper.PdfViewerTextMarkupAnnotationMode.None && DetectStrokes(new PointF((float)position.X, (float)position.Y), false) != StrikeType.None) || _documentViewWrapper.SelectedAnnotation is InkSignatureWrapper || _documentViewWrapper.SelectedAnnotation is PdfViewerStampAnnotationWrapper || _documentViewWrapper.SelectedAnnotation is PdfViewerInkAnnotationWrapper || _documentViewWrapper.SelectedAnnotation is PdfViewerStickyNoteAnnotationWrapper || _documentViewWrapper.SelectedAnnotation is PdfViewerPolygonAnnotationWrapper || _documentViewWrapper.SelectedAnnotation is PolylineAnnotationWrapper)
                                return;

                            // Custom
                            if (_documentViewWrapper.SelectedAnnotation == null)
                            {
                                _pdfDocumentViewWrapper.OnPageClickedEx(pageIndex, _canvas);

                                if (_pdfDocumentViewWrapper.AreaSelectionEnabled)
                                {
                                    StartAreaSelection(e);
                                }
                                return;
                            }
                            _documentViewWrapper.SelectedAnnotation = null;
                        }
                        else
                        {
                            m_lastDragPoint = pointF;
                            m_isDragInProgress = true;
                            _documentViewWrapper.IsDragInProgress = true;
                        }
                    }
                }
            }
        }

        private void SelectionCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            bool isSetHand = false;

            if (_isAreaSelectionMouseDown)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    AddAreaSelectionRectangle();
                }
                else
                {
                    EndAreaSelection();
                }
            }

            if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.InkEraser)
            {
                _canvas.Cursor = LocalizationManagerWrapper.Eraser;
            }
            else
            {
                if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.Stamp)
                    return;
                bool flag1 = true;
                if (_documentView != null && _pdfDocumentView != null)
                {
                    PageMouseMoveEventArgs eventArgs = PageMouseMoveEventArgsWrapper.CreatePageMouseMoveEventArgs(CurrentPageIndex, e.GetPosition(_canvas));
                    _pdfDocumentViewWrapper.OnPageMouseMove(eventArgs);
                }
                PointF pointF = _unitConvertor.ConvertFromPixels(new PointF((float)e.GetPosition(_canvas).X, (float)e.GetPosition(_canvas).Y), PdfGraphicsUnit.Point);
                if (_page.Rotation == 90.0)
                    pointF = RemoveRotations(pointF, 90.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                else if (_page.Rotation == 180.0)
                    pointF = RemoveRotations(pointF, 180.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                else if (_page.Rotation == 270.0)
                    pointF = RemoveRotations(pointF, 270.0, new SizeF(_documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Width * (float)_documentView.ZoomFactor, _documentView.LoadedDocument.Pages[CurrentPageIndex].Size.Height * (float)_documentView.ZoomFactor));
                if (_pdfDocumentViewWrapper.m_isPopupFocused || m_isDragging)
                    return;
                bool flag2 = RedactionOnMouseMove(pointF);
                if (_pdfDocumentViewWrapper.InkMode != PdfDocumentViewWrapper.PdfViewerInkAnnotationMode.None || _pdfDocumentViewWrapper.TextMarkupMode != PdfDocumentViewWrapper.PdfViewerTextMarkupAnnotationMode.None)
                {
                    bool flag3;
                    if (_pdfDocumentViewWrapper.InkMode == PdfDocumentViewWrapper.PdfViewerInkAnnotationMode.Ink && !m_isRectangleMouseDown && _documentViewWrapper.InkAnnotations.ContainsKey(CurrentPageIndex) && _pdfDocumentViewWrapper.TextMarkupMode == PdfDocumentViewWrapper.PdfViewerTextMarkupAnnotationMode.None)
                    {
                        if (DetectInkOnMove(e) != StrikeType.None)
                        {
                            _canvas.Cursor = System.Windows.Input.Cursors.Hand;
                            isSetHand = true;
                        }
                        else
                            _canvas.Cursor = System.Windows.Input.Cursors.Cross;
                        flag3 = false;
                    }
                    if (_pdfDocumentView.AnnotationMode != PdfDocumentView.PdfViewerAnnotationMode.InkEraser || !_documentViewWrapper.InkAnnotations.ContainsKey(CurrentPageIndex) || _pdfDocumentViewWrapper.TextMarkupMode != PdfDocumentViewWrapper.PdfViewerTextMarkupAnnotationMode.None)
                        return;
                    _canvas.Cursor = LocalizationManagerWrapper.Eraser;
                    flag3 = false;
                }
                else
                {
                    bool flag4 = false;
                    if (m_isDragInProgress && _documentViewWrapper.CurrentAnnotation != null && !_documentViewWrapper.CurrentAnnotation.ReadOnly && !_documentViewWrapper.CurrentAnnotation.Hidden && !_documentViewWrapper.CurrentAnnotation.IsLocked)
                    {
                        _documentView.SelectedBounds.Clear();
                        double num1 = ((double)pointF.X - (double)m_lastDragPoint.X) / _documentView.ZoomFactor;
                        double num2 = ((double)pointF.Y - (double)m_lastDragPoint.Y) / _documentView.ZoomFactor;
                        if (_documentViewWrapper.CurrentShape != DocumentViewWrapper.PdfViewerShape.Line && _documentViewWrapper.CurrentShape != DocumentViewWrapper.PdfViewerShape.Arrow)
                        {
                            switch (MouseStrikeType)
                            {
                                case StrikeType.Body:
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.X += (float)num1;
                                        startingPoint.Y += (float)num2;
                                        _documentViewWrapper.startingPoint = startingPoint;

                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.X += (float)num1;
                                        EndingPoint.Y += (float)num2;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                                case StrikeType.TopLeftStrike:
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.X += (float)num1;
                                        startingPoint.Y += (float)num2;
                                        _documentViewWrapper.startingPoint = startingPoint;
                                    }
                                    break;
                                case StrikeType.TopRightStrike:
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.Y += (float)num2;
                                        _documentViewWrapper.startingPoint = startingPoint;

                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.X += (float)num1;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                                case StrikeType.BottomRightStrike:
                                    {
                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.X += (float)num1;
                                        EndingPoint.Y += (float)num2;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                                case StrikeType.BottomLeftStrike:
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.X += (float)num1;
                                        _documentViewWrapper.startingPoint = startingPoint;

                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.Y += (float)num2;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                                case StrikeType.LeftStrike:
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.X += (float)num1;
                                        _documentViewWrapper.startingPoint = startingPoint;
                                    }
                                    break;
                                case StrikeType.RightStrike:
                                    {
                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.X += (float)num1;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                                case StrikeType.TopStrike:
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.Y += (float)num2;
                                        _documentViewWrapper.startingPoint = startingPoint;
                                    }
                                    break;
                                case StrikeType.BottomStrike:
                                    {
                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.Y += (float)num2;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                            }
                        }
                        else if (_documentViewWrapper.PolygonPoints.Count == 0)
                        {
                            switch (MouseStrikeType)
                            {
                                case StrikeType.Body:
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.X += (float)num1;
                                        startingPoint.Y += (float)num2;
                                        _documentViewWrapper.startingPoint = startingPoint;

                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.X += (float)num1;
                                        EndingPoint.Y += (float)num2;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                                case StrikeType.TopLeftStrike:
                                case StrikeType.TopRightStrike:
                                case StrikeType.BottomRightStrike:
                                case StrikeType.BottomLeftStrike:
                                    if (_documentViewWrapper.ActivePoint == DocumentViewWrapper.ActiveLinePoint.LineStartPoint)
                                    {
                                        var startingPoint = _documentViewWrapper.startingPoint;
                                        startingPoint.X += (float)num1;
                                        startingPoint.Y += (float)num2;
                                        _documentViewWrapper.startingPoint = startingPoint;
                                        break;
                                    }
                                    {
                                        var EndingPoint = _documentViewWrapper.EndingPoint;
                                        EndingPoint.X += (float)num1;
                                        EndingPoint.Y += (float)num2;
                                        _documentViewWrapper.EndingPoint = EndingPoint;
                                    }
                                    break;
                            }
                        }
                        m_lastDragPoint = pointF;
                        _canvas.InvalidateVisual();
                    }
                    else if (_documentViewWrapper.PolygonPoints.Count == 0 && _documentViewWrapper.Shapes.ContainsKey(CurrentPageIndex))
                    {
                        IList shape = _documentViewWrapper.Shapes[CurrentPageIndex];
                        for (int index = 0; index < shape.Count; ++index)
                        {
                            PdfViewerShapeAnnotationWrapper shapeAnnotationWrapper = new(shape[index]);

                            if (!shapeAnnotationWrapper.Hidden && (shapeAnnotationWrapper.Layer == null || shapeAnnotationWrapper.Layer.Visible))
                            {
                                MouseStrikeType = SetStrikeType(GetPointOnZoom(shapeAnnotationWrapper.StartingPoint), GetPointOnZoom(shapeAnnotationWrapper.EndPoint), pointF, shapeAnnotationWrapper.AnnotationShape);
                                if (MouseStrikeType != StrikeType.None)
                                {
                                    flag4 = true;
                                    break;
                                }
                            }
                        }
                        if (_documentView.SelectedBounds.Count > 0)
                        {
                            SetExpectedCursor();
                            flag1 = false;
                        }
                        else
                        {
                            if (MouseStrikeType != StrikeType.None)
                            {
                                _canvas.Cursor = System.Windows.Input.Cursors.Hand;
                                isSetHand = true;
                            }
                            else if (!flag2)
                                SetExpectedCursor();
                            flag1 = false;
                        }
                    }
                    if (flag2)
                    {
                        _canvas.Cursor = Cursors.Hand;
                        isSetHand = true;
                        flag1 = false;
                    }
                    else if (_pdfDocumentView.PageRedactor.EnableRedactionMode)
                    {
                        if (flag1 && _documentView.Cursor == Cursors.IBeam)
                        {
                            _canvas.Cursor = _documentView.Cursor;
                            flag1 = false;
                        }
                        else if (!flag4)
                        {
                            if (_documentView.Cursor == Cursors.IBeam)
                                _canvas.Cursor = _documentView.Cursor;
                            else
                                _canvas.Cursor = Cursors.Cross;
                            flag1 = false;
                        }
                    }
                    if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.Polygon && MouseStrikeType == StrikeType.None)
                    {
                        if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation is PdfViewerPolygonAnnotationWrapper selectedAnnotation && selectedAnnotation.IsAnnotationResized && m_isRectangleMouseDown)
                            _canvas.Cursor = Cursors.Arrow;
                        if (!m_isRectangleMouseDown)
                            _canvas.Cursor = Cursors.Cross;
                    }
                    if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.Polyline && MouseStrikeType == StrikeType.None)
                    {
                        if (_documentViewWrapper.SelectedAnnotation != null)
                        {
                            if (_documentViewWrapper.SelectedAnnotation is PolylineAnnotationWrapper selectedAnnotation1 && !selectedAnnotation1.Hidden && !selectedAnnotation1.IsLocked && !selectedAnnotation1.ReadOnly)
                            {
                                if (selectedAnnotation1.IsAnnotationMoved)
                                    _canvas.Cursor = Cursors.SizeAll;
                                else if (selectedAnnotation1.IsAnnotationResized)
                                    _canvas.Cursor = Cursors.Arrow;
                                else
                                    _canvas.Cursor = Cursors.Cross;
                            }
                        }
                        else
                            _canvas.Cursor = Cursors.Cross;
                    }
                    else if (_documentViewWrapper.SelectedAnnotation != null && _documentViewWrapper.SelectedAnnotation is PolylineAnnotationWrapper selectedAnnotation2)
                    {
                        if (!selectedAnnotation2.IsAnnotationMoved && !_pdfDocumentViewWrapper.IsPanning)
                        {
                            if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.None)
                                _canvas.Cursor = _documentView.Cursor;
                        }
                        else if (!selectedAnnotation2.IsAnnotationMoved && _pdfDocumentViewWrapper.IsPanning && !selectedAnnotation2.IsAnnotationResized)
                            _canvas.Cursor = _pdfDocumentView.Cursor;
                        else if (selectedAnnotation2.IsAnnotationResized && _pdfDocumentViewWrapper.IsPanning)
                            _canvas.Cursor = Cursors.Arrow;
                    }
                    if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.StickyNote && MouseStrikeType == StrikeType.None)
                    {
                        if (!_documentViewWrapper.IsTocOrHyperlinkHOvered)
                            _canvas.Cursor = LocalizationManagerWrapper.StickyNoteCursor;
                        else
                            _canvas.Cursor = _documentView.Cursor;
                    }
                    else if (flag1 && !m_isRectangleMouseDown && _documentViewWrapper.SelectedAnnotation == null && !_documentViewWrapper.IsShapeSelected && _documentView.Parent != null && !_pdfDocumentViewWrapper.IsPanTouchedDown && _pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.None)
                        _canvas.Cursor = _documentView.Cursor;

                    if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.Stamp && (_pdfDocumentViewWrapper.IsStandardStampSelected || _pdfDocumentViewWrapper.IsCustomImageSelected) || _pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.FreeText || _pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.HandwrittenSignature)
                        _canvas.Cursor = Cursors.SizeAll;

                    #region Custom
                    if (_canvas.Cursor == Cursors.Hand && !isSetHand)
                    {
                        if (_documentViewWrapper.CheckMarkupBounds(pointF, false))
                        {
                        }
                        else
                        {
                            _canvas.Cursor = _documentView.Cursor;
                        }
                    }
                    #endregion
                }
            }

            if (_pdfDocumentViewWrapper.AreaSelectionEnabled)
            {
                if (_documentViewWrapper.IsAnnotationHovered)
                {
                    _pdfDocumentView.Cursor = null;
                }
                else
                {
                    _pdfDocumentView.Cursor = Cursors.Cross;
                }
            }
        }

        private void SelectionCanvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (_pdfDocumentView.AnnotationMode == PdfDocumentView.PdfViewerAnnotationMode.InkEraser && this.m_compositelog)
            {
                this.m_compositelog = false;
                ++_documentViewWrapper.CompositeBufferCount;
                _documentViewWrapper.AddAnnotationHistory(null, PdfDocumentViewWrapper.PdfViewerActionType.None, CurrentPageIndex, null, null, TransactionState.End);
            }
            if (e.LeftButton == MouseButtonState.Pressed)
                return;
            this.m_isRectangleMouseDown = false;
            if (_pdfDocumentViewWrapper.m_focusedFreeTextAnnotation != null)
            {
                if (_documentViewWrapper.m_selectedFreeTextAnnotation != null)
                {
                    if (_pdfDocumentViewWrapper.m_focusedFreeTextAnnotation.Id != _documentViewWrapper.m_selectedFreeTextAnnotation.Id && System.Windows.Controls.Panel.GetZIndex((UIElement)_pdfDocumentViewWrapper.m_focusedFreeTextAnnotation.TextBox) == 2)
                        System.Windows.Controls.Panel.SetZIndex((UIElement)_pdfDocumentViewWrapper.m_focusedFreeTextAnnotation.TextBox, 1);
                }
                else if (_pdfDocumentViewWrapper.m_focusedFreeTextAnnotation.TextBox.IsReadOnly && System.Windows.Controls.Panel.GetZIndex((UIElement)_pdfDocumentViewWrapper.m_focusedFreeTextAnnotation.TextBox) == 2)
                    System.Windows.Controls.Panel.SetZIndex((UIElement)_pdfDocumentViewWrapper.m_focusedFreeTextAnnotation.TextBox, 1);
            }
            if (!this.isFreeTextResized && !_documentViewWrapper.IsFreeTextSelected)
            {
                if (_documentViewWrapper.m_selectedFreeTextAnnotation != null && !_documentViewWrapper.m_selectedFreeTextAnnotation.isPopUpWindowOpened)
                {
                    this.m_isFromMouseUp = true;
                    (_documentViewWrapper.m_selectedFreeTextAnnotation.TextBox.Parent as InkCanvas)?.GetWrapper()?.DeselectFreeTextAnnotation();
                }
            }
            else
                this.isFreeTextResized = false;
            if (_canvas.EditingMode == InkCanvasEditingMode.Select)
                return;
            if (_documentViewWrapper.IsShapeSelected)
            {
                this.AddSelectBounds();
            }
            else
            {
                this._documentView.SelectedBounds.Clear();
                _documentViewWrapper.CurrentAnnotation = null;
            }
            if (this.m_canInvokeSelection)
            {
                AnnotationWrapper annotation = null;
                if (_documentViewWrapper.CurrentAnnotation != null)
                    annotation = _documentViewWrapper.CurrentAnnotation;
                else if (_documentViewWrapper.SelectedAnnotation != null)
                    annotation = _documentViewWrapper.SelectedAnnotation;
                _documentViewWrapper.InvokeSelectEvent(annotation);
                this.m_canInvokeSelection = false;
            }
            if (_pdfDocumentView.PageRedactor.EnableRedactionMode && this.m_isRedactionEnded && !_documentViewWrapper.IsShapeSelected && _documentViewWrapper.StartingPoint != PointF.Empty && _documentViewWrapper.EndingPoint != PointF.Empty)
            {
                if ((double)_documentViewWrapper.StartingPoint.X < 0.0)
                {
                    var point = _documentViewWrapper.startingPoint;
                    point.X = this._unitConvertor.ConvertFromPixels((float)_documentViewWrapper.PagesEx[this.CurrentPageIndex].c_gapBetweenPages, PdfGraphicsUnit.Point);
                    _documentViewWrapper.startingPoint = point;
                }

                if ((double)_documentViewWrapper.StartingPoint.Y < 0.0)
                {
                    var point = _documentViewWrapper.startingPoint;
                    point.Y = this._unitConvertor.ConvertFromPixels((float)_documentViewWrapper.PagesEx[this.CurrentPageIndex].c_gapBetweenPages, PdfGraphicsUnit.Point);
                    _documentViewWrapper.startingPoint = point;
                }

                this.AdjustXPosition();
                PointF location = new PointF(Math.Min(_documentViewWrapper.StartingPoint.X, _documentViewWrapper.EndingPoint.X), Math.Min(_documentViewWrapper.StartingPoint.Y, _documentViewWrapper.EndingPoint.Y));
                PointF pointF = new PointF(Math.Max(_documentViewWrapper.StartingPoint.X, _documentViewWrapper.EndingPoint.X), Math.Max(_documentViewWrapper.StartingPoint.Y, _documentViewWrapper.EndingPoint.Y));
                //TODO
                //      RedactionWrapper redaction = new Redaction(this.m_documentView);
                //      redaction.Rotation = this._page.Rotation;
                //      this.SetRedactSettings(redaction.RedactionSettings);
                //      redaction.AnnotationPopup = new PdfAnnotationPopup((IAnnotation)redaction);
                //      _canvas.Children.Add((UIElement)redaction.AnnotationPopup);
                //      redaction.Bounds = new RectangleF[1]
                //      {
                //new RectangleF(location, new SizeF(pointF.X - location.X, pointF.Y - location.Y))
                //      };
                //      redaction.PageIndex = this.CurrentPageIndex;
                //      List<int> intList = new List<int>();
                //      if (this.m_documentView.Pages[this.CurrentPageIndex].PageGlyphs.ContainsKey(this.CurrentPageIndex) && this.m_documentView.Pages[this.CurrentPageIndex].PageGlyphs[this.CurrentPageIndex].Count > 0)
                //      {
                //          foreach (Syncfusion.Windows.PdfViewer.TextSelection textSelection in this.m_documentView.Pages[this.CurrentPageIndex].PageGlyphs[this.CurrentPageIndex].Values)
                //          {
                //              if (redaction.Bounds[0].Contains(textSelection.GlyphRect))
                //                  intList.Add(textSelection.TextIndex);
                //          }
                //      }
                //      if (_pdfDocumentView.RegionsToBeRedacted.ContainsKey(this.CurrentPageIndex))
                //          _pdfDocumentView.RegionsToBeRedacted[this.CurrentPageIndex].Add(redaction, intList);
                //      else
                //          _pdfDocumentView.RegionsToBeRedacted.Add(this.CurrentPageIndex, new Dictionary<Redaction, List<int>>()
                //{
                //  {
                //    redaction,
                //    intList
                //  }
                //});
                //      _pdfDocumentView.IsRedacted = true;
                //      if (_pdfDocumentView != null && _pdfDocumentView.pdfViewerControl != null && _pdfDocumentView.pdfViewerControl.RedactToolbar != null && _pdfDocumentView.pdfViewerControl.RedactToolbar.ApplyRedactButton != null)
                //          _pdfDocumentView.pdfViewerControl.RedactToolbar.ApplyRedactButton.IsEnabled = true;
                //      this.m_documentView.RedoStack.Clear();
                //      this.m_documentView.UndoStack.Add(new AnnotationHistory((IAnnotation)null, PdfDocumentView.PdfViewerActionType.Add, this.CurrentPageIndex, (LayerHistory)null, new RedactionHistory(redaction, (List<int>)null)));
                //      if ((double)this.m_documentView.UndoStack.Count - this.m_documentView.CompositeBufferCount > (double)_pdfDocumentView.UndoRedoLimit)
                //          this.m_documentView.RemoveElementFromUndoStack();
                //      this.m_isDragging = false;
            }
            if (_documentViewWrapper.StartingPoint != PointF.Empty && _documentViewWrapper.EndingPoint != PointF.Empty && _documentViewWrapper.StartingPoint != _documentViewWrapper.EndingPoint)
            {
                if ((double)_documentViewWrapper.StartingPoint.X < 0.0)
                {
                    var point = _documentViewWrapper.startingPoint;
                    point.X = 0.0f + this._unitConvertor.ConvertFromPixels((float)_documentViewWrapper.PagesEx[this.CurrentPageIndex].c_gapBetweenPages, PdfGraphicsUnit.Point);
                    _documentViewWrapper.startingPoint = point;
                }

                if ((double)_documentViewWrapper.StartingPoint.Y < 0.0)
                {
                    var point = _documentViewWrapper.startingPoint;
                    point.Y = 0.0f + this._unitConvertor.ConvertFromPixels((float)_documentViewWrapper.PagesEx[this.CurrentPageIndex].c_gapBetweenPages, PdfGraphicsUnit.Point);
                    _documentViewWrapper.startingPoint = point;
                }

                this.AdjustXPosition();
                DocumentViewWrapper.PdfViewerShape shapeName = _pdfDocumentViewWrapper.ShapeMode != PdfDocumentViewWrapper.PdfViewerShapeAnnotationMode.Line ? (_pdfDocumentViewWrapper.ShapeMode != PdfDocumentViewWrapper.PdfViewerShapeAnnotationMode.Rectangle ? (_pdfDocumentViewWrapper.ShapeMode != PdfDocumentViewWrapper.PdfViewerShapeAnnotationMode.Circle ? (_pdfDocumentViewWrapper.ShapeMode != PdfDocumentViewWrapper.PdfViewerShapeAnnotationMode.Arrow ? _documentViewWrapper.CurrentShape : DocumentViewWrapper.PdfViewerShape.Arrow) : DocumentViewWrapper.PdfViewerShape.Circle) : DocumentViewWrapper.PdfViewerShape.Rectangle) : DocumentViewWrapper.PdfViewerShape.Line;
                if (shapeName != DocumentViewWrapper.PdfViewerShape.None)
                {
                    this.AddShapes(_documentViewWrapper.CanvasPageIndex, shapeName);
                    _canvas.InvalidateVisual();
                }
                this.m_isDragging = false;
            }
            else if (_documentViewWrapper.StartingPoint != PointF.Empty && _documentViewWrapper.EndingPoint != PointF.Empty && _documentViewWrapper.StartingPoint == _documentViewWrapper.EndingPoint)
                this.m_isDragging = false;
            if (_documentViewWrapper.IsShapeSelected)
            {
                _documentViewWrapper.SelectedStartPoint = _documentViewWrapper.StartingPoint;
                _documentViewWrapper.SelectedEndPoint = _documentViewWrapper.EndingPoint;
            }
            _documentViewWrapper.StartingPoint = PointF.Empty;
            _documentViewWrapper.EndingPoint = PointF.Empty;
            _documentViewWrapper.CurrentShape = DocumentViewWrapper.PdfViewerShape.None;
            _documentViewWrapper.CanvasPageIndex = -1;
            this.m_isDragInProgress = false;
            _documentViewWrapper.IsDragInProgress = false;
            if (_pdfDocumentView.CursorMode != PdfViewerCursorMode.HandTool || _canvas.Cursor != LocalizationManagerWrapper.PanPressed)
                return;
            _canvas.Cursor = LocalizationManagerWrapper.PanHover;
        }

        /// <summary>
        /// StrikeType
        /// </summary>
        public enum StrikeType
        {
            None,
            Body,
            TopLeftStrike,
            TopRightStrike,
            BottomRightStrike,
            BottomLeftStrike,
            LeftStrike,
            RightStrike,
            TopStrike,
            BottomStrike,
        }
    }
}
